package com.example.seakdreamandroid.whiteNoise;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.AudioInfo;

import java.util.List;

import okhttp3.Request;

/*
杨锐涵 白噪音的自由组合的实体类的Adapter
 */
public class WhiteNoiseFreeConbinationChannelAdapter extends BaseAdapter {
    //上下文环境
    private Context mContext;
    //数据源
    private List<AudioInfo> channels;

    public WhiteNoiseFreeConbinationChannelAdapter(Context mContext, List<AudioInfo> channels) {
        this.mContext = mContext;
        this.channels = channels;
    }
    //返回数据源中元素的数量
    @Override
    public int getCount() {
        return channels.size();
    }
    //返回数据源中指定序号的数据对象
    @Override
    public Object getItem(int position) {
        return channels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    private static class ViewHolder{
        //每一个item中的控制
        ImageView imageView;
        TextView name;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if(view == null){
            //加载item的布局文件，生成一个view对象，再给view赋值
            view = LayoutInflater.from(mContext).inflate(R.layout.item_whitenoise_gridview,null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = view.findViewById(R.id.white_noise_gridview_image);
            viewHolder.name = view.findViewById(R.id.white_noise_gridview_name);

            //将viewHolder对象同view关联起来
            view.setTag(viewHolder);
        }else{
            //重复利用view对象
            //获取到跟当前convertView绑定在一起的viewholder对象
            viewHolder = (ViewHolder) view.getTag();
        }
        AudioInfo channel = channels.get(position);

        RequestOptions requestOptions = new RequestOptions();
        Glide.with(mContext)
                .load(channel.getAudioPic())
                .apply(requestOptions)
                .into(viewHolder.imageView);

        //viewHolder.name.setText(channel.getAudioName());

        if(channel.getIfVip().equals("1")){
            viewHolder.name.setText(channel.getAudioName()+"(vip)");
            viewHolder.name.setTextColor(Color.RED);
        }else{
            viewHolder.name.setText(channel.getAudioName());
            viewHolder.name.setTextColor(Color.BLACK);
        }

        return view;
    }
}
